#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import os
import sys

execfile(os.path.join(sys.path[0], "framework.py"))

from oriolus.gaffer.gaffer.selectstrategy import LatestStrategy
from oriolus.pith.packets.packet import FullName
from unittest import TestCase, TestLoader, TextTestRunner

class TestSelectStrategy(TestCase):
    def test_latest_select1(self):
        input = []
        expected = []

        self.__assert_latest_strategy_works(input, expected)

    def test_latest_select2(self):
        input = [FullName("packet", "1.2.3.4"), FullName("packet", "1.3.5.6")]
        expected = [FullName("packet", "1.3.5.6"), ]

        self.__assert_latest_strategy_works(input, expected)

    def test_latest_select3(self):
        input = [FullName("packet", "1.3.5.6"), FullName("packet", "1.2.3.4")]
        expected = [FullName("packet", "1.3.5.6"), ]

        self.__assert_latest_strategy_works(input, expected)

    def test_latest_select4(self):
        input = [FullName("packet1", "1.3.5.6"), FullName("packet2", "1.2.3.4"), FullName("packet1", "1.2.3.4")]
        expected = [FullName("packet1", "1.3.5.6"), FullName("packet2", "1.2.3.4")]

        self.__assert_latest_strategy_works(input, expected)

    def test_latest_select5(self):
        input = [FullName("packet2", "1.2.3.4"), FullName("packet1", "1.3.5.6"), FullName("packet1", "1.2.3.4")]
        expected = [FullName("packet2", "1.2.3.4"), FullName("packet1", "1.3.5.6")]

        self.__assert_latest_strategy_works(input, expected)

    def test_latest_select6(self):
        input = [FullName("packet", "1.2.3.4"), ]
        expected = [FullName("packet", "1.2.3.4"), ]

        self.__assert_latest_strategy_works(input, expected)

    def test_latest_select7(self):
        input = [FullName("packet", "1.2.3.4"), FullName("packet", "1.2.3.4")]
        expected = [FullName("packet", "1.2.3.4"), ]

        self.__assert_latest_strategy_works(input, expected)

    def test_latest_select8(self):
        input = [FullName("packet1", "1.2.3.1"), FullName("packet2", "1.2.0.0"), FullName("packet1", "1.2.3.10")]
        expected = [FullName("packet1", "1.2.3.10"), FullName("packet2", "1.2.0.0")]

        self.__assert_latest_strategy_works(input, expected)

    def __assert_latest_strategy_works(self, input, expected):
        strategy = LatestStrategy()
        actual = strategy.select_packets(input)

        self.assertEqual(expected, actual)

if __name__ == "__main__":
    suite = TestLoader().loadTestsFromTestCase(TestSelectStrategy)
    TextTestRunner(verbosity=2).run(suite)